Examples > Scripting a Database Object |
You can also use Schema Compare SDK to generate the T-SQL script for a database object. The script can be of the type CREATE, DROP, ALTER (when ALTER is supported by the object), or a combination of the above.
The following example shows how to script the table Employees from the AdventureWorks database.
using xSQL.Schema.Core; using xSQL.Schema.SqlServer; using xSQL.SchemaCompare.SqlServer; namespace xSQL.Sdk.SchemaCompare.Examples { class Scripting { /// <summary> /// This method reads the schema of the database AdventureWorks and scripts the table Employee. /// </summary> public static void Script() { SqlServer server; SqlDatabase database; SqlTable table; ScriptingOptions options; try { //--create the SQL Server object server = new SqlServer(@"(local)"); //--create the database object database = server.GetDatabase("AdventureWorks"); //--attach an event handler to database.SchemaOperation event in order to get progress information during the schema read database.SchemaOperation += new EventHandler<SchemaOperationEventArgs>(database_SchemaOperation); //--read the database schema database.ReadSchema(); //--create scripting options; options = new ScriptingOptions(); options.CreateScript = true; options.DropScript = false; options.AlterScript = false; //--locate and script the Employee table table = database.SqlTables["HumanResources", "Employee"]; if (table != null) Console.Write(table.GetScript(options)); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } } private static void database_SchemaOperation(object sender, SchemaOperationEventArgs e) { //--exclude verbose messages if (e.Message.MessageType != OperationMessageTypeEnum.Verbose) Console.WriteLine(e.Message.Text); } } }